package com.phospherus.test

import com.phospherus.flow.entity.*
import com.phospherus.flow.service.*
import com.phospherus.user.entity.*
import com.phospherus.user.service.*
import org.junit.Test
import org.springframework.beans.factory.annotation.Autowired

/**
 * @author songjinlong
 * @version 1.0
 * @since 2017/12/3
 */
class DataInit : BaseTest() {
    @Autowired
    lateinit var roleService: RoleService
    @Autowired
    lateinit var flowService: FlowService
    @Autowired
    lateinit var flowNodeRefService: FlowNodeRefService
    @Autowired
    lateinit var flowNodeService: FlowNodeService
    @Autowired
    lateinit var resourceService: ResourceService
    @Autowired
    lateinit var roleResourceService: RoleResourceService
    @Autowired
    lateinit var userService: UserService
    @Autowired
    lateinit var orgService: OrgService
    @Autowired
    lateinit var formService: FormService
    @Autowired
    lateinit var formItemPermissionService: FormItemPermissionService

    var 业务员: Long = 1
    var 业务领导: Long = 2
    var 单证员: Long = 3
    var 单证领导: Long = 4
    var 风控员: Long = 5
    var 财务: Long = 6
    var 管理员: Long = 7
    @Test
    fun initBaseData() {
        initRole()
        initResource()
        initRoleResource()
        initAdminUser()
        initOrgAndUser()
        initFlow()
    }

    @Test
    fun initAdminUser() {
        var user = User(userName = "管理员", account = "admin", isLeader = false, orgId = -1)
        var roleIds = listOf("$管理员")
        var (u, passWord) = userService.createUser(user, roleIds)
        userService.modifyPassWord(passWord, "123456", u)
    }

    @Test
    fun initOrgAndUser() {
        var 公司 = orgService.save(Org(orgName = "公司", pid = -1))
        var 业务部 = orgService.save(Org(orgName = "业务部", pid = 公司.id))
        var 运营部 = orgService.save(Org(orgName = "运营部", pid = 公司.id))
        var 业务一部 = orgService.save(Org(orgName = "业务一部", pid = 业务部.id))
        var 业务二部 = orgService.save(Org(orgName = "业务二部", pid = 业务部.id))
        var 业务三部 = orgService.save(Org(orgName = "业务三部", pid = 业务部.id))
        var 单证 = orgService.save(Org(orgName = "单证", pid = 运营部.id))
        var 风控 = orgService.save(Org(orgName = "风控", pid = 运营部.id))
        var 财务ORG = orgService.save(Org(orgName = "财务", pid = 运营部.id))
        //init user
        var 张霁雯 = User(userName = "张霁雯", account = "zhangjiwen", isLeader = true, orgId = 公司.id)
        var 张霁雯Roles = listOf("$管理员")
        userService.createUser(张霁雯, 张霁雯Roles)
        var 赵燕 = User(userName = "赵燕", account = "zhaoyan", isLeader = true, orgId = 公司.id)
        var 赵燕Roles = listOf("$管理员", "$单证领导")
        userService.createUser(赵燕, 赵燕Roles)
        var 陈丽丽 = User(userName = "陈丽丽", account = "chenlili", isLeader = true, orgId = 业务一部.id)
        var 陈丽丽Roles = listOf("$业务领导")
        userService.createUser(陈丽丽, 陈丽丽Roles)
        var 刘薇 = User(userName = "刘薇", account = "liuwei", isLeader = false, orgId = 业务一部.id)
        var 刘薇Roles = listOf("$业务员")
        userService.createUser(刘薇, 刘薇Roles)
        var 张传红 = User(userName = "张传红", account = "zhangchuanhong", isLeader = false, orgId = 业务一部.id)
        var 张传红Roles = listOf("$业务员")
        userService.createUser(张传红, 张传红Roles)
        var 吴玉桂 = User(userName = "吴玉桂", account = "wuyugui", isLeader = false, orgId = 业务一部.id)
        var 吴玉桂Roles = listOf("$业务员")
        userService.createUser(吴玉桂, 吴玉桂Roles)
        var 王丽霞 = User(userName = "王丽霞", account = "wanglixia", isLeader = false, orgId = 业务一部.id)
        var 王丽霞Roles = listOf("$业务员")
        userService.createUser(王丽霞, 王丽霞Roles)
        var 钟秀娟 = User(userName = "钟秀娟", account = "zhongxiujuan", isLeader = false, orgId = 单证.id)
        var 钟秀娟Roles = listOf("$单证员")
        userService.createUser(钟秀娟, 钟秀娟Roles)
        var 宋峥 = User(userName = "宋峥", account = "songzheng", isLeader = true, orgId = 财务ORG.id)
        var 宋峥Roles = listOf("$财务")
        userService.createUser(宋峥, 宋峥Roles)
        var 黄杰 = User(userName = "黄杰", account = "huangjie", isLeader = true, orgId = 风控.id)
        var 黄杰Roles = listOf("$风控员")
        userService.createUser(黄杰, 黄杰Roles)
    }

    @Test
    fun initRole() {
        var roles = arrayOf("业务员", "业务领导", "单证员", "单证领导", "风控员", "财务", "管理员")
        roleService.save(roles.mapTo(ArrayList<Role>(), { Role(roleName = it) }))
    }

    @Test
    fun initResource() {
        var itemNames = arrayOf("发起流程", "待发事项", "待办事项", "已办事项", "订单查询", "客户管理", "客户授信", "用户管理", "企业查询")
        var resources = itemNames.mapIndexedTo(ArrayList(), { i, v -> Resource(resourceId = "item_0${i + 1}", resourceName = v) })
        resourceService.save(resources)
    }

    @Test
    fun initRoleResource() {
        var itemIds = arrayOf(1, 2, 3, 4, 5, 6, 7, 8, 9)
        var 管理员资源 = itemIds.mapTo(ArrayList(), { RoleResource(resourceId = it.toLong(), roleId = 管理员) })
        roleResourceService.save(管理员资源)
        var 业务员资源 = itemIds.filter { it < 7  }.mapTo(ArrayList(), { RoleResource(resourceId = it.toLong(), roleId = 业务员) })
        roleResourceService.save(业务员资源)
        var 单证员资源 = itemIds.filter { it in 3..5 }.mapTo(ArrayList(), { RoleResource(resourceId = it.toLong(), roleId = 单证员) })
        roleResourceService.save(单证员资源)
        var 单证领导资源 = itemIds.filter { it in 3..5 }.mapTo(ArrayList(), { RoleResource(resourceId = it.toLong(), roleId = 单证领导) })
        roleResourceService.save(单证领导资源)
        var 业务领导资源 = itemIds.filter { it in 3..5 }.mapTo(ArrayList(), { RoleResource(resourceId = it.toLong(), roleId = 业务领导) })
        roleResourceService.save(业务领导资源)
        var 财务资源 = itemIds.filter { it in 3..5 }.mapTo(ArrayList(), { RoleResource(resourceId = it.toLong(), roleId = 财务) })
        roleResourceService.save(财务资源)
        var 风控资源 = itemIds.filter { it in 3..5 || it == 7 || it == 9 }.mapTo(ArrayList(), { RoleResource(resourceId = it.toLong(), roleId = 风控员) })
        roleResourceService.save(风控资源)
    }

    @Test
    fun initFlow() {
        //1
        业务提单流程()
        //2
        买家付定金给TCF()
        //3
        TCF付定金给工厂()
        //4
        TCF付尾款到工厂()
        //5
        单证流程()
        //6
        买家付尾款给TCF()
    }

    //流程1
    fun 业务提单流程() {
        var flow = flowService.save(Flow(flowName = "业务提单（生成订单）", flowPath = "businessform"))
        var nodes = listOf(
                FlowNode(flowNodeName = "业务填报合同信息", roleId = 业务员, flowId = flow.id, nodeType = 0),
                FlowNode(flowNodeName = "单证审批合同信息", roleId = 单证员, flowId = flow.id),
                FlowNode(flowNodeName = "业务领导审批合同信息", roleId = 业务领导, flowId = flow.id),
                FlowNode(flowNodeName = "风控审批合同信息", roleId = 风控员, flowId = flow.id),
                FlowNode(flowNodeName = "财务审批合同信息", roleId = 财务, flowId = flow.id),
                FlowNode(flowNodeName = "结束", flowId = flow.id, nodeType = 2))
        flowNodeService.save(nodes)
        var refs = listOf(
                FlowNodeRef(refName = "送单证审批", pFlowNodeId = nodes[0].id, cFlowNodeId = nodes[1].id, flowId = flow.id),
                FlowNodeRef(refName = "取消流程", pFlowNodeId = nodes[0].id, cFlowNodeId = nodes[5].id, flowId = flow.id),
                FlowNodeRef(refName = "退回业务填报", pFlowNodeId = nodes[1].id, cFlowNodeId = nodes[0].id, flowId = flow.id),
                FlowNodeRef(refName = "送业务领导审批", pFlowNodeId = nodes[1].id, cFlowNodeId = nodes[2].id, flowId = flow.id),
                FlowNodeRef(refName = "送风控审批", pFlowNodeId = nodes[2].id, cFlowNodeId = nodes[3].id, flowId = flow.id),
                FlowNodeRef(refName = "退回业务填报", pFlowNodeId = nodes[2].id, cFlowNodeId = nodes[0].id, flowId = flow.id),
                FlowNodeRef(refName = "送财务审批", pFlowNodeId = nodes[3].id, cFlowNodeId = nodes[4].id, flowId = flow.id),
                FlowNodeRef(refName = "退回业务填报", pFlowNodeId = nodes[3].id, cFlowNodeId = nodes[0].id, flowId = flow.id),
                FlowNodeRef(refName = "结束", pFlowNodeId = nodes[4].id, cFlowNodeId = nodes[5].id, flowId = flow.id),
                FlowNodeRef(refName = "退回业务填报", pFlowNodeId = nodes[4].id, cFlowNodeId = nodes[0].id, flowId = flow.id))
        flowNodeRefService.save(refs)

        var items = listOf("businessform_businesscheckform",
                "businessform_foreigntradecontract",
                "businessform_pi",
                "businessform_domestictradecontract",
                "businessform_profitcalculation",
                "item01_01",
                "item01_02",
                "item01_03",
                "item01_04")
        var form = 业务提单表单(flow.id!!)
        var 业务填报合同信息ROLE = items.mapIndexedTo(ArrayList(), { index, s ->
            FormItemPermission(formId = form.id,
                    flowId = flow.id,
                    flowNodeId = nodes[0].id,
                    flowNodeName = nodes[0].flowNodeName,
                    itemName = s,
                    permission = if (index < 5) {
                        "W"
                    } else {
                        "R"
                    })

        })
        formItemPermissionService.save(业务填报合同信息ROLE)
        var 单证审批合同信息ROLE = items.mapIndexedTo(ArrayList(), { index, s ->
            FormItemPermission(formId = form.id,
                    flowId = flow.id,
                    flowNodeId = nodes[1].id,
                    flowNodeName = nodes[1].flowNodeName,
                    itemName = s,
                    permission = if (index ==5 ) {
                        "W"
                    } else {
                        "R"
                    })

        })
        formItemPermissionService.save(单证审批合同信息ROLE)
        var 业务领导审批合同信息ROLE = items.mapIndexedTo(ArrayList(), { index, s ->
            FormItemPermission(formId = form.id,
                    flowId = flow.id,
                    flowNodeId = nodes[2].id,
                    flowNodeName = nodes[2].flowNodeName,
                    itemName = s,
                    permission = if (index == 6) {
                        "W"
                    } else {
                        "R"
                    })

        })
        formItemPermissionService.save(业务领导审批合同信息ROLE)
        var 风控审批合同信息ROLE = items.mapIndexedTo(ArrayList(), { index, s ->
            FormItemPermission(formId = form.id,
                    flowId = flow.id,
                    flowNodeId = nodes[3].id,
                    flowNodeName = nodes[3].flowNodeName,
                    itemName = s,
                    permission = if (index == 7) {
                        "W"
                    } else {
                        "R"
                    })

        })
        formItemPermissionService.save(风控审批合同信息ROLE)
        var 财务审批合同信息ROLE = items.mapIndexedTo(ArrayList(), { index, s ->
            FormItemPermission(formId = form.id,
                    flowId = flow.id,
                    flowNodeId = nodes[4].id,
                    flowNodeName = nodes[4].flowNodeName,
                    itemName = s,
                    permission = if (index == 8) {
                        "W"
                    } else {
                        "R"
                    })

        })
        formItemPermissionService.save(财务审批合同信息ROLE)
    }

    fun 业务提单表单(flowId: Long): Form = formService.save(Form(formName = "业务提单（生成订单）", formServiceName = "business_lading", flowId = flowId, flowPath = "businessform"))
    //流程2
    fun 买家付定金给TCF表单(flowId: Long): Form = formService.save(Form(formName = "买家付定金给TCF", formServiceName = "buyertotcf", flowId = flowId, flowPath = "buyertotcf"))

    fun 买家付定金给TCF() {
        var flow = flowService.save(Flow(flowName = "买家付定金给TCF", flowPath = "buyertotcf"))
        var nodes = listOf(
                FlowNode(flowNodeName = "业务填报", roleId = 业务员, flowId = flow.id, nodeType = 0),
                FlowNode(flowNodeName = "业务领导审批", roleId = 业务领导, flowId = flow.id),
                FlowNode(flowNodeName = "财务审批", roleId = 财务, flowId = flow.id),
                FlowNode(flowNodeName = "结束", flowId = flow.id, nodeType = 2))
        flowNodeService.save(nodes)
        var refs = listOf(
                FlowNodeRef(refName = "送业务领导审批", pFlowNodeId = nodes[0].id, cFlowNodeId = nodes[1].id, flowId = flow.id),
                FlowNodeRef(refName = "取消流程", pFlowNodeId = nodes[0].id, cFlowNodeId = nodes[3].id, flowId = flow.id),
                FlowNodeRef(refName = "退回业务填报", pFlowNodeId = nodes[1].id, cFlowNodeId = nodes[0].id, flowId = flow.id),
                FlowNodeRef(refName = "送财务审批", pFlowNodeId = nodes[1].id, cFlowNodeId = nodes[2].id, flowId = flow.id),
                FlowNodeRef(refName = "结束", pFlowNodeId = nodes[2].id, cFlowNodeId = nodes[3].id, flowId = flow.id),
                FlowNodeRef(refName = "退回业务填报", pFlowNodeId = nodes[2].id, cFlowNodeId = nodes[0].id, flowId = flow.id))
        flowNodeRefService.save(refs)

        var form = 买家付定金给TCF表单(flow.id!!)
        var items = listOf("buyertotcf_bindFContracts", "buyertotcf_bindDContracts")

        var 业务填报ROLE = items.mapTo(ArrayList(), {
            FormItemPermission(formId = form.id,
                    flowId = flow.id,
                    flowNodeId = nodes[0].id,
                    flowNodeName = nodes[0].flowNodeName,
                    itemName = it,
                    permission = "W")

        })
        业务填报ROLE.add(FormItemPermission(formId = form.id,
                flowId = flow.id,
                flowNodeId = nodes[0].id,
                flowNodeName = nodes[0].flowNodeName,
                itemName = "item_02_01",
                permission = "R"))
        业务填报ROLE.add(FormItemPermission(formId = form.id,
                flowId = flow.id,
                flowNodeId = nodes[0].id,
                flowNodeName = nodes[0].flowNodeName,
                itemName = "item_02_02",
                permission = "R"))
        formItemPermissionService.save(业务填报ROLE)

        var 业务领导审批ROLE = items.mapTo(ArrayList(), {
            FormItemPermission(formId = form.id,
                    flowId = flow.id,
                    flowNodeId = nodes[1].id,
                    flowNodeName = nodes[1].flowNodeName,
                    itemName = it,
                    permission = "R")

        })
        业务领导审批ROLE.add(FormItemPermission(formId = form.id,
                flowId = flow.id,
                flowNodeId = nodes[1].id,
                flowNodeName = nodes[1].flowNodeName,
                itemName = "item_02_01",
                permission = "W"))
        业务领导审批ROLE.add(FormItemPermission(formId = form.id,
                flowId = flow.id,
                flowNodeId = nodes[1].id,
                flowNodeName = nodes[1].flowNodeName,
                itemName = "item_02_02",
                permission = "R"))
        formItemPermissionService.save(业务领导审批ROLE)

        var 财务审批ROLE = items.mapTo(ArrayList(), {
            FormItemPermission(formId = form.id,
                    flowId = flow.id,
                    flowNodeId = nodes[2].id,
                    flowNodeName = nodes[2].flowNodeName,
                    itemName = it,
                    permission = "R")

        })
        财务审批ROLE.add(FormItemPermission(formId = form.id,
                flowId = flow.id,
                flowNodeId = nodes[2].id,
                flowNodeName = nodes[2].flowNodeName,
                itemName = "item_02_01",
                permission = "R"))
        财务审批ROLE.add(FormItemPermission(formId = form.id,
                flowId = flow.id,
                flowNodeId = nodes[2].id,
                flowNodeName = nodes[2].flowNodeName,
                itemName = "item_02_02",
                permission = "W"))
        formItemPermissionService.save(财务审批ROLE)
    }

    //流程3
    fun TCF付定金给工厂表单(flowId: Long): Form = formService.save(Form(formName = "TCF付定金给工厂", formServiceName = "tcftofactory", flowId = flowId, flowPath = "tcftofactory"))

    fun TCF付定金给工厂() {
        var flow = flowService.save(Flow(flowName = "TCF付定金给工厂", flowPath = "tcftofactory"))
        var nodes = listOf(
                FlowNode(flowNodeName = "业务填报", roleId = 业务员, flowId = flow.id, nodeType = 0),
                FlowNode(flowNodeName = "业务领导审批", roleId = 业务领导, flowId = flow.id),
                FlowNode(flowNodeName = "财务审批", roleId = 财务, flowId = flow.id),
                FlowNode(flowNodeName = "结束", flowId = flow.id, nodeType = 2))
        flowNodeService.save(nodes)
        var refs = listOf(
                FlowNodeRef(refName = "送业务领导审批", pFlowNodeId = nodes[0].id, cFlowNodeId = nodes[1].id, flowId = flow.id),
                FlowNodeRef(refName = "取消流程", pFlowNodeId = nodes[0].id, cFlowNodeId = nodes[3].id, flowId = flow.id),
                FlowNodeRef(refName = "退回业务填报", pFlowNodeId = nodes[1].id, cFlowNodeId = nodes[0].id, flowId = flow.id),
                FlowNodeRef(refName = "送财务审批", pFlowNodeId = nodes[1].id, cFlowNodeId = nodes[2].id, flowId = flow.id),
                FlowNodeRef(refName = "结束", pFlowNodeId = nodes[2].id, cFlowNodeId = nodes[3].id, flowId = flow.id),
                FlowNodeRef(refName = "退回业务填报", pFlowNodeId = nodes[2].id, cFlowNodeId = nodes[0].id, flowId = flow.id))
        flowNodeRefService.save(refs)

        var form = TCF付定金给工厂表单(flow.id!!)
        var items = listOf(
                "tcftofactory_applicant",
                "tcftofactory_applicationDate",
                "tcftofactory_applicationDepart",
                "tcftofactory_checkedPayMethods",
                "tcftofactory_collectionCompany",
                "tcftofactory_collectionBank",
                "tcftofactory_collectionAccount",
                "tcftofactory_paymentAmount",
                "tcftofactory_paymentBigAmount",
                "tcftofactory_paymentAbstract",
                "tcftofactory_businessType",
                "tcftofactory_businessTypeoptions",
                "tcftofactory_signDate",
                "tcftofactory_contractAmount",
                "tcftofactory_payedMount",
                "tcftofactory_thisPaymentTypes",
                "tcftofactory_nonPaymentTypes",
                "tcftofactory_thisPaymentAmount",
                "tcftofactory_nonPaymentAmount",
                "tcftofactory_dtcNO")

        var 业务填报ROLE = items.mapTo(ArrayList(), {
            FormItemPermission(formId = form.id,
                    flowId = flow.id,
                    flowNodeId = nodes[0].id,
                    flowNodeName = nodes[0].flowNodeName,
                    itemName = it,
                    permission = "W")

        })
        业务填报ROLE.add(FormItemPermission(formId = form.id,
                flowId = flow.id,
                flowNodeId = nodes[0].id,
                flowNodeName = nodes[0].flowNodeName,
                itemName = "item_03_01",
                permission = "R"))
        业务填报ROLE.add(FormItemPermission(formId = form.id,
                flowId = flow.id,
                flowNodeId = nodes[0].id,
                flowNodeName = nodes[0].flowNodeName,
                itemName = "item_03_02",
                permission = "R"))
        formItemPermissionService.save(业务填报ROLE)

        var 业务领导审批ROLE = items.mapTo(ArrayList(), {
            FormItemPermission(formId = form.id,
                    flowId = flow.id,
                    flowNodeId = nodes[1].id,
                    flowNodeName = nodes[1].flowNodeName,
                    itemName = it,
                    permission = "R")

        })
        业务领导审批ROLE.add(FormItemPermission(formId = form.id,
                flowId = flow.id,
                flowNodeId = nodes[1].id,
                flowNodeName = nodes[1].flowNodeName,
                itemName = "item_03_01",
                permission = "W"))
        业务领导审批ROLE.add(FormItemPermission(formId = form.id,
                flowId = flow.id,
                flowNodeId = nodes[1].id,
                flowNodeName = nodes[1].flowNodeName,
                itemName = "item_03_02",
                permission = "R"))
        formItemPermissionService.save(业务领导审批ROLE)

        var 财务审批ROLE = items.mapTo(ArrayList(), {
            FormItemPermission(formId = form.id,
                    flowId = flow.id,
                    flowNodeId = nodes[2].id,
                    flowNodeName = nodes[2].flowNodeName,
                    itemName = it,
                    permission = "R")

        })
        财务审批ROLE.add(FormItemPermission(formId = form.id,
                flowId = flow.id,
                flowNodeId = nodes[2].id,
                flowNodeName = nodes[2].flowNodeName,
                itemName = "item_03_01",
                permission = "R"))
        财务审批ROLE.add(FormItemPermission(formId = form.id,
                flowId = flow.id,
                flowNodeId = nodes[2].id,
                flowNodeName = nodes[2].flowNodeName,
                itemName = "item_03_02",
                permission = "W"))
        formItemPermissionService.save(财务审批ROLE)


    }

    //流程4
    fun TCF付尾款到工厂表单(flowId: Long): Form = formService.save(Form(formName = "TCF付尾款到工厂表单", formServiceName = "tcflefttofactory", flowId = flowId, flowPath = "tcflefttofactory"))

    fun TCF付尾款到工厂() {
        var flow = flowService.save(Flow(flowName = "TCF付尾款到工厂", flowPath = "tcflefttofactory"))
        var nodes = listOf(
                FlowNode(flowNodeName = "业务填报", roleId = 业务员, flowId = flow.id, nodeType = 0),
                FlowNode(flowNodeName = "业务领导审批", roleId = 业务领导, flowId = flow.id),
                FlowNode(flowNodeName = "风控审批", roleId = 风控员, flowId = flow.id),
                FlowNode(flowNodeName = "财务审批", roleId = 财务, flowId = flow.id),
                FlowNode(flowNodeName = "结束", flowId = flow.id, nodeType = 2))
        flowNodeService.save(nodes)
        var refs = listOf(
                FlowNodeRef(refName = "送业务领导审批", pFlowNodeId = nodes[0].id, cFlowNodeId = nodes[1].id, flowId = flow.id),
                FlowNodeRef(refName = "取消流程", pFlowNodeId = nodes[0].id, cFlowNodeId = nodes[4].id, flowId = flow.id),
                FlowNodeRef(refName = "送风控审批", pFlowNodeId = nodes[1].id, cFlowNodeId = nodes[2].id, flowId = flow.id),
                FlowNodeRef(refName = "退回业务填报", pFlowNodeId = nodes[1].id, cFlowNodeId = nodes[0].id, flowId = flow.id),
                FlowNodeRef(refName = "送财务审批", pFlowNodeId = nodes[2].id, cFlowNodeId = nodes[3].id, flowId = flow.id),
                FlowNodeRef(refName = "退回业务填报", pFlowNodeId = nodes[2].id, cFlowNodeId = nodes[0].id, flowId = flow.id),
                FlowNodeRef(refName = "结束", pFlowNodeId = nodes[3].id, cFlowNodeId = nodes[4].id, flowId = flow.id),
                FlowNodeRef(refName = "退回业务填报", pFlowNodeId = nodes[3].id, cFlowNodeId = nodes[0].id, flowId = flow.id)
        )
        flowNodeRefService.save(refs)

        var form = TCF付尾款到工厂表单(flow.id!!)
        var items = listOf("tcflefttofactory_qualityassurance", "tcflefttofactory_leftapply")

        var 业务填报ROLE = items.mapTo(ArrayList(), {
            FormItemPermission(formId = form.id,
                    flowId = flow.id,
                    flowNodeId = nodes[0].id,
                    flowNodeName = nodes[0].flowNodeName,
                    itemName = it,
                    permission = "W")

        })
        业务填报ROLE.add(FormItemPermission(formId = form.id,
                flowId = flow.id,
                flowNodeId = nodes[0].id,
                flowNodeName = nodes[0].flowNodeName,
                itemName = "item_04_01",
                permission = "R"))
        业务填报ROLE.add(FormItemPermission(formId = form.id,
                flowId = flow.id,
                flowNodeId = nodes[0].id,
                flowNodeName = nodes[0].flowNodeName,
                itemName = "item_04_02",
                permission = "R"))
        业务填报ROLE.add(FormItemPermission(formId = form.id,
                flowId = flow.id,
                flowNodeId = nodes[0].id,
                flowNodeName = nodes[0].flowNodeName,
                itemName = "item_04_03",
                permission = "R"))
        formItemPermissionService.save(业务填报ROLE)

        var 业务领导审批ROLE = items.mapTo(ArrayList(), {
            FormItemPermission(formId = form.id,
                    flowId = flow.id,
                    flowNodeId = nodes[1].id,
                    flowNodeName = nodes[1].flowNodeName,
                    itemName = it,
                    permission = "R")

        })
        业务领导审批ROLE.add(FormItemPermission(formId = form.id,
                flowId = flow.id,
                flowNodeId = nodes[1].id,
                flowNodeName = nodes[1].flowNodeName,
                itemName = "item_04_01",
                permission = "W"))
        业务领导审批ROLE.add(FormItemPermission(formId = form.id,
                flowId = flow.id,
                flowNodeId = nodes[1].id,
                flowNodeName = nodes[1].flowNodeName,
                itemName = "item_04_02",
                permission = "R"))
        业务领导审批ROLE.add(FormItemPermission(formId = form.id,
                flowId = flow.id,
                flowNodeId = nodes[1].id,
                flowNodeName = nodes[1].flowNodeName,
                itemName = "item_04_03",
                permission = "R"))
        formItemPermissionService.save(业务领导审批ROLE)


        var 风控审批ROLE = items.mapTo(ArrayList(), {
            FormItemPermission(formId = form.id,
                    flowId = flow.id,
                    flowNodeId = nodes[2].id,
                    flowNodeName = nodes[2].flowNodeName,
                    itemName = it,
                    permission = "R")

        })
        风控审批ROLE.add(FormItemPermission(formId = form.id,
                flowId = flow.id,
                flowNodeId = nodes[2].id,
                flowNodeName = nodes[2].flowNodeName,
                itemName = "item_04_01",
                permission = "R"))
        风控审批ROLE.add(FormItemPermission(formId = form.id,
                flowId = flow.id,
                flowNodeId = nodes[2].id,
                flowNodeName = nodes[2].flowNodeName,
                itemName = "item_04_02",
                permission = "W"))
        风控审批ROLE.add(FormItemPermission(formId = form.id,
                flowId = flow.id,
                flowNodeId = nodes[2].id,
                flowNodeName = nodes[2].flowNodeName,
                itemName = "item_04_03",
                permission = "R"))
        formItemPermissionService.save(风控审批ROLE)

        var 财务审批ROLE = items.mapTo(ArrayList(), {
            FormItemPermission(formId = form.id,
                    flowId = flow.id,
                    flowNodeId = nodes[3].id,
                    flowNodeName = nodes[3].flowNodeName,
                    itemName = it,
                    permission = "R")

        })
        财务审批ROLE.add(FormItemPermission(formId = form.id,
                flowId = flow.id,
                flowNodeId = nodes[3].id,
                flowNodeName = nodes[3].flowNodeName,
                itemName = "item_04_01",
                permission = "R"))
        财务审批ROLE.add(FormItemPermission(formId = form.id,
                flowId = flow.id,
                flowNodeId = nodes[3].id,
                flowNodeName = nodes[3].flowNodeName,
                itemName = "item_04_02",
                permission = "R"))
        财务审批ROLE.add(FormItemPermission(formId = form.id,
                flowId = flow.id,
                flowNodeId = nodes[3].id,
                flowNodeName = nodes[3].flowNodeName,
                itemName = "item_04_03",
                permission = "W"))
        formItemPermissionService.save(财务审批ROLE)
    }

    //流程5
    fun 单证流程表单(flowId: Long): Form = formService.save(Form(formName = "单证流程", formServiceName = "documentprocess", flowId = flowId, flowPath = "documentprocess"))

    fun 单证流程() {
        var flow = flowService.save(Flow(flowName = "单证流程", flowPath = "documentprocess"))
        var nodes = listOf(
                FlowNode(flowNodeName = "业务填报发货通知单", roleId = 业务员, flowId = flow.id, nodeType = 0),
                FlowNode(flowNodeName = "单证填报出厂确认单", roleId = 单证员, flowId = flow.id),
                FlowNode(flowNodeName = "单证领导审批出厂确认", roleId = 单证领导, flowId = flow.id),
                FlowNode(flowNodeName = "风控审批出厂确认", roleId = 风控员, flowId = flow.id),
                FlowNode(flowNodeName = "单证填报报关合同", roleId = 单证员, flowId = flow.id),
                FlowNode(flowNodeName = "单证领导审批报关合同", roleId = 单证领导, flowId = flow.id),
                FlowNode(flowNodeName = "单证填报开票信息", roleId = 单证员, flowId = flow.id),
                FlowNode(flowNodeName = "单证领导审批开票信息", roleId = 单证领导, flowId = flow.id),
                FlowNode(flowNodeName = "单证填报核销资料", roleId = 单证员, flowId = flow.id),
                FlowNode(flowNodeName = "单证领导审批核销资料", roleId = 单证领导, flowId = flow.id),
                FlowNode(flowNodeName = "结束", flowId = flow.id, nodeType = 2))
        flowNodeService.save(nodes)
        var refs = listOf(
                FlowNodeRef(refName = "送单证填报出厂确认单", pFlowNodeId = nodes[0].id, cFlowNodeId = nodes[1].id, flowId = flow.id),
                FlowNodeRef(refName = "取消流程", pFlowNodeId = nodes[0].id, cFlowNodeId = nodes[10].id, flowId = flow.id),

                FlowNodeRef(refName = "送单证领导审批出厂确认单", pFlowNodeId = nodes[1].id, cFlowNodeId = nodes[2].id, flowId = flow.id),
                FlowNodeRef(refName = "退回业务填报", pFlowNodeId = nodes[1].id, cFlowNodeId = nodes[0].id, flowId = flow.id),

                FlowNodeRef(refName = "送风控审批出厂确认单", pFlowNodeId = nodes[2].id, cFlowNodeId = nodes[3].id, flowId = flow.id),
                FlowNodeRef(refName = "退回业务填报", pFlowNodeId = nodes[2].id, cFlowNodeId = nodes[0].id, flowId = flow.id),

                FlowNodeRef(refName = "送单证填报报关合同", pFlowNodeId = nodes[3].id, cFlowNodeId = nodes[4].id, flowId = flow.id),
                FlowNodeRef(refName = "退回业务填报", pFlowNodeId = nodes[3].id, cFlowNodeId = nodes[0].id, flowId = flow.id),

                FlowNodeRef(refName = "送单证领导审批报关合同", pFlowNodeId = nodes[4].id, cFlowNodeId = nodes[5].id, flowId = flow.id),
                FlowNodeRef(refName = "退回业务填报", pFlowNodeId = nodes[4].id, cFlowNodeId = nodes[0].id, flowId = flow.id),

                FlowNodeRef(refName = "送单证填报开票信息", pFlowNodeId = nodes[5].id, cFlowNodeId = nodes[6].id, flowId = flow.id),
                FlowNodeRef(refName = "退回单证填报报关合同", pFlowNodeId = nodes[5].id, cFlowNodeId = nodes[4].id, flowId = flow.id),

                FlowNodeRef(refName = "送单证领导审批开票信息", pFlowNodeId = nodes[6].id, cFlowNodeId = nodes[7].id, flowId = flow.id),
                FlowNodeRef(refName = "退回业务填报", pFlowNodeId = nodes[6].id, cFlowNodeId = nodes[0].id, flowId = flow.id),

                FlowNodeRef(refName = "送单证填报核销资料", pFlowNodeId = nodes[7].id, cFlowNodeId = nodes[8].id, flowId = flow.id),
                FlowNodeRef(refName = "退回单证填报报开票信息", pFlowNodeId = nodes[7].id, cFlowNodeId = nodes[6].id, flowId = flow.id),

                FlowNodeRef(refName = "送单证领导审批核销资料", pFlowNodeId = nodes[8].id, cFlowNodeId = nodes[9].id, flowId = flow.id),
                FlowNodeRef(refName = "退回业务填报", pFlowNodeId = nodes[8].id, cFlowNodeId = nodes[0].id, flowId = flow.id),

                FlowNodeRef(refName = "送单证填报核销资料", pFlowNodeId = nodes[9].id, cFlowNodeId = nodes[8].id, flowId = flow.id),
                FlowNodeRef(refName = "送结束", pFlowNodeId = nodes[9].id, cFlowNodeId = nodes[10].id, flowId = flow.id)
        )
        flowNodeRefService.save(refs)

        var form = 单证流程表单(flow.id!!)
        var items = arrayOf("item05_01", "item05_02", "item05_03", "item05_04", "item05_05",
                "documentprocess_fd", "documentprocess_cc", "documentprocess_askInvoice", "documentprocess_collectionData",
                "documentprocess_fd_notice", "documentprocess_fd_confirm", "documentprocess_cc_letterOfDelivery", "documentprocess_cc_customsDeclarationInvoice",
                "documentprocess_cc_customsDeclarationBox", "documentprocess_cc_customsDeclarationContract", "documentprocess_cc_declarationForm", "documentprocess_cc_declarationElements"
        )
        var roleList = arrayOf(
                "R,R,W,R,R,R,R,R,R,R",
                "R,R,R,W,R,R,R,R,R,R",
                "R,R,R,R,R,W,R,R,R,R",
                "R,R,R,R,R,R,R,W,R,R",
                "R,R,R,R,R,R,R,R,R,W",
                "R,R,R,R,R,R,R,R,R,R",
                "H,H,H,H,R,R,R,R,R,R",
                "H,H,H,H,H,H,W,R,R,R",
                "H,H,H,H,H,H,H,H,W,R",
                "W,R,R,R,R,R,R,R,R,R",
                "R,W,R,R,R,R,R,R,R,R",
                "R,R,R,R,W,R,R,R,R,R",
                "R,R,R,R,W,R,R,R,R,R",
                "R,R,R,R,W,R,R,R,R,R",
                "R,R,R,R,W,R,R,R,R,R",
                "R,R,R,R,W,R,R,R,R,R",
                "R,R,R,R,W,R,R,R,R,R")

        //todo  初始化权限
        items.forEachIndexed { index, value ->
            run {
                var roles = roleList[index].split(",")
                var r = mutableListOf<FormItemPermission>()
                roles.forEachIndexed { i, v ->
                    run {
                        r.add(FormItemPermission(formId = form.id,
                                flowId = flow.id,
                                flowNodeId = nodes[i].id,
                                flowNodeName = nodes[i].flowNodeName,
                                itemName = value,
                                permission = v))
                    }
                }
                formItemPermissionService.save(r)
            }
        }
    }

    //流程6
    fun 买家付尾款给TCF表单(flowId: Long): Form = formService.save(Form(formName = "买家付尾款给TCF", formServiceName = "buyerlefttotcf", flowId = flowId, flowPath = "buyerlefttotcf"))

    fun 买家付尾款给TCF() {
        var flow = flowService.save(Flow(flowName = "买家付尾款给TCF", flowPath = "buyerlefttotcf"))
        var nodes = listOf(
                FlowNode(flowNodeName = "业务填报", roleId = 业务员, flowId = flow.id, nodeType = 0),
                FlowNode(flowNodeName = "业务领导审批", roleId = 业务领导, flowId = flow.id),
                FlowNode(flowNodeName = "财务审批", roleId = 财务, flowId = flow.id),
                FlowNode(flowNodeName = "结束", flowId = flow.id, nodeType = 2))
        flowNodeService.save(nodes)
        var refs = listOf(
                FlowNodeRef(refName = "送业务领导审批", pFlowNodeId = nodes[0].id, cFlowNodeId = nodes[1].id, flowId = flow.id),
                FlowNodeRef(refName = "取消流程", pFlowNodeId = nodes[0].id, cFlowNodeId = nodes[3].id, flowId = flow.id),
                FlowNodeRef(refName = "退回业务填报", pFlowNodeId = nodes[1].id, cFlowNodeId = nodes[0].id, flowId = flow.id),
                FlowNodeRef(refName = "送财务审批", pFlowNodeId = nodes[1].id, cFlowNodeId = nodes[2].id, flowId = flow.id),
                FlowNodeRef(refName = "结束", pFlowNodeId = nodes[2].id, cFlowNodeId = nodes[3].id, flowId = flow.id),
                FlowNodeRef(refName = "退回业务填报", pFlowNodeId = nodes[2].id, cFlowNodeId = nodes[0].id, flowId = flow.id))
        flowNodeRefService.save(refs)

        var form = 买家付定金给TCF表单(flow.id!!)
        var items = listOf("buyerlefttotcf_bindFContracts", "buyerlefttotcf_bindDContracts")
//      "item_06_01", "item_06_02"

        var 业务填报ROLE = items.mapTo(ArrayList(), {
            FormItemPermission(formId = form.id,
                    flowId = flow.id,
                    flowNodeId = nodes[0].id,
                    flowNodeName = nodes[0].flowNodeName,
                    itemName = it,
                    permission = "W")

        })
        业务填报ROLE.add(FormItemPermission(formId = form.id,
                flowId = flow.id,
                flowNodeId = nodes[0].id,
                flowNodeName = nodes[0].flowNodeName,
                itemName = "item_06_01",
                permission = "R"))
        业务填报ROLE.add(FormItemPermission(formId = form.id,
                flowId = flow.id,
                flowNodeId = nodes[0].id,
                flowNodeName = nodes[0].flowNodeName,
                itemName = "item_06_02",
                permission = "R"))
        formItemPermissionService.save(业务填报ROLE)

        var 业务领导审批ROLE = items.mapTo(ArrayList(), {
            FormItemPermission(formId = form.id,
                    flowId = flow.id,
                    flowNodeId = nodes[1].id,
                    flowNodeName = nodes[1].flowNodeName,
                    itemName = it,
                    permission = "R")

        })
        业务领导审批ROLE.add(FormItemPermission(formId = form.id,
                flowId = flow.id,
                flowNodeId = nodes[1].id,
                flowNodeName = nodes[1].flowNodeName,
                itemName = "item_06_01",
                permission = "W"))
        业务领导审批ROLE.add(FormItemPermission(formId = form.id,
                flowId = flow.id,
                flowNodeId = nodes[1].id,
                flowNodeName = nodes[1].flowNodeName,
                itemName = "item_06_02",
                permission = "R"))
        formItemPermissionService.save(业务领导审批ROLE)

        var 财务审批ROLE = items.mapTo(ArrayList(), {
            FormItemPermission(formId = form.id,
                    flowId = flow.id,
                    flowNodeId = nodes[2].id,
                    flowNodeName = nodes[2].flowNodeName,
                    itemName = it,
                    permission = "R")

        })
        财务审批ROLE.add(FormItemPermission(formId = form.id,
                flowId = flow.id,
                flowNodeId = nodes[2].id,
                flowNodeName = nodes[2].flowNodeName,
                itemName = "item_06_01",
                permission = "R"))
        财务审批ROLE.add(FormItemPermission(formId = form.id,
                flowId = flow.id,
                flowNodeId = nodes[2].id,
                flowNodeName = nodes[2].flowNodeName,
                itemName = "item_06_02",
                permission = "W"))
        formItemPermissionService.save(财务审批ROLE)

    }
}
